import pygame

SIZE = width, height = 288, 512
FPS = 60
# 重力加速度
g = 0.1
# 水管出现事件
CREAT_PIPE_EVENT = pygame.USEREVENT
# 飞行速度
fly_speed = 2
# 标题
title_image = pygame.image.load('image/title.png')
title_rect = title_image.get_rect()
title_rect.center = width / 2, 100
# GAME OVER
over_image = pygame.image.load('image/text_game_over.png')
over_rect = over_image.get_rect()
over_rect.center = width / 2, 100
# 教程提示
tutorial_image = pygame.image.load('image/tutorial.png')
tutorial_rect = tutorial_image.get_rect()
tutorial_rect.center = width / 2, 250


class BackGround(pygame.sprite.Sprite):
    """游戏背景"""

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load('image/bg_day.png')
        self.rect = self.image.get_rect()


class Land(pygame.sprite.Sprite):
    """地面"""

    def __init__(self, is_alt=False):
        super().__init__()
        self.image = pygame.image.load('image/land.png')
        self.rect = self.image.get_rect()
        if is_alt:
            self.rect.left = self.rect.width
        self.rect.bottom = height
        self.speed = fly_speed

    def update(self):
        self.rect.x -= self.speed
        # 两张地面的图片循环移动
        if self.rect.right <= 0:
            self.rect.left = self.rect.width


class Bird(pygame.sprite.Sprite):
    """玩家控制的小鸟"""

    def __init__(self):
        super().__init__()
        self.image_list = [pygame.image.load(f'image/bird0_{i}.png') for i in range(3)]
        self.image_index = 0
        self.image = self.image_list[self.image_index]
        self.rect = self.image.get_rect()
        self.rect.center = 144, 170
        self.y = float(self.rect.y)
        self.speed = 0
        self.is_down = True
        self.frame = 1

    def reset(self):
        self.rect.center = 72, 100
        self.y = float(self.rect.y)
        self.speed = 0
        self.is_down = True

    def update(self):
        self.y += self.speed
        self.rect.y = self.y
        if self.is_down:
            self.speed += g  # 自由落体
        # 限制小鸟不会飞出屏幕顶端
        elif self.rect.y < 0:
            self.y = 0
            self.rect.y = 0
            self.speed = 0
        else:
            self.speed = -2
        self.wave()

    def wave(self):
        # 小鸟挥动翅膀的动画
        if self.frame < FPS / 10:
            self.frame += 1
        else:
            self.image_index += 1
            self.frame = 1
            if self.image_index >= 3:
                self.image_index = 0
            self.image = self.image_list[self.image_index]


class Button:
    """按钮类"""

    def __init__(self, image_name, text='按钮'):
        self.text = text
        self.image = pygame.image.load(image_name)
        self.rect = self.image.get_rect()
        self.active = False
        self.function = None

    def draw(self, surface):
        surface.blit(self.image, self.rect)

    def get_click(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                mx, my = event.pos
                if self.rect.left <= mx <= self.rect.right and self.rect.top <= my <= self.rect.bottom:
                    self.rect.y += 1
                    self.active = True
        elif event.type == pygame.MOUSEBUTTONUP and self.active:
            if event.button == 1:
                mx, my = event.pos
                self.rect.y -= 1
                if self.rect.left <= mx <= self.rect.right and self.rect.top <= my <= self.rect.bottom:
                    if callable(self.function):
                        self.function()
                self.active = False

    def click_connect(self, function):
        self.function = function


class Pipe(pygame.sprite.Sprite):
    """水管"""

    def __init__(self, is_down=False):
        super().__init__()
        if is_down:
            self.image = pygame.image.load('image/pipe_down.png')
        else:
            self.image = pygame.image.load('image/pipe_up.png')
        self.rect = self.image.get_rect()
        self.rect.x = width
        self.speed = fly_speed

    def update(self):
        self.rect.x -= self.speed
        if self.rect.right < 0:
            self.kill()
